﻿using RBACDemo.Ability.Docking.RoleAuthApp.Dtos;
using Youshow.Ace.Ability.Dtos;

namespace RBACDemo.Ability.Docking.UserApp.Dtos;

public class UserDto : BaseModelDto<long>
{
    public string UserName { get; set; }
    public List<RoleDto> Roles { get; set; }
    public List<ApiAuthDto> ApiAuths { get; set; } = new();

    public static List<UserDto> SetApiAuths(List<UserDto> userDtos)
    {
        userDtos.ForEach(m =>
        {
            m.SetApiAuths();
        });
        return userDtos;
    }

    public void SetApiAuths()
    {
        var apiAuthsList = Roles.Select(m => m.ApiAuths);
        foreach (var apiAuths in apiAuthsList)
        {
            ApiAuths.AddRange(apiAuths);
        }

        ApiAuths = ApiAuths.DistinctBy(m => m.Id).ToList();
    }
}